﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2017/7/8
 * 时间: 0:57
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;

namespace TIDTool
{
	/// <summary>
	/// Description of ByteUtils.
	/// </summary>
	public class ByteUtils
	{
		public ByteUtils()
		{
		}
		
		public static bool BytesSet(byte[] array, int beginPos, int value, bool isLittleEndian)
		{
			if (isLittleEndian)
			{
				array[beginPos+0] = (byte)((value >> 0) & 0xFF);
				array[beginPos+1] = (byte)((value >> 8) & 0xFF);
				array[beginPos+2] = (byte)((value >> 16) & 0xFF);
				array[beginPos+3] = (byte)((value >> 24) & 0xFF);
			}
			else
			{
				array[beginPos+0] = (byte)((value >> 24) & 0xFF);
				array[beginPos+1] = (byte)((value >> 16) & 0xFF);
				array[beginPos+2] = (byte)((value >> 8) & 0xFF);
				array[beginPos+3] = (byte)((value >> 0) & 0xFF);
			}
			return true;
		}
		
		public static bool BytesSet(byte[] array, int beginPos, uint value, bool isLittleEndian)
		{
			if (isLittleEndian)
			{
				array[beginPos+0] = (byte)((value >> 0) & 0xFF);
				array[beginPos+1] = (byte)((value >> 8) & 0xFF);
				array[beginPos+2] = (byte)((value >> 16) & 0xFF);
				array[beginPos+3] = (byte)((value >> 24) & 0xFF);
			}
			else
			{
				array[beginPos+0] = (byte)((value >> 24) & 0xFF);
				array[beginPos+1] = (byte)((value >> 16) & 0xFF);
				array[beginPos+2] = (byte)((value >> 8) & 0xFF);
				array[beginPos+3] = (byte)((value >> 0) & 0xFF);
			}
			return true;
		}
		
		public static int BytesGetInt(byte[] array, int beginPos, bool isLittleEndian)
		{
			int ret = 0;
			if (isLittleEndian)
			{
				ret = array[beginPos] + 
				array[beginPos+1]*256 + 
				array[beginPos+2]*256*256 + 
				array[beginPos+3]*256*256*256;
			}
			else
			{
				ret = array[beginPos]*256*256*256 + 
				array[beginPos+1]*256*256 + 
				array[beginPos+2]*256 + 
				array[beginPos+3];
			}
			return ret;
		}
	}
}
